<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Personal_health_info_model extends CI_Model
{
	function __construct()
	{
		parent::__construct();
	}

	public function get_history_by($uid, $item)
	{
		$sql = 'SELECT DISTINCT '.$item.', date FROM (`userhealthinfo`) WHERE uid="'.$uid.'" AND '.$item.' is not NULL ORDER BY date DESC LIMIT 20';
		$query = $this->db->query($sql);
		return $query->result();
	}

	public function get_last_symptoms($custom_id)
	{
		$sql = 'SELECT * FROM (`usersymptom`) WHERE uid="'.$custom_id.'" AND endDate is NULL';
		$query = $this->db->query($sql);
		return $query->result();
	}

	public function get_current_health_info($uid)
	{

		$sql = 'SELECT * FROM (`userhealthinfo`) WHERE uid="'.$uid.'" AND date=(SELECT max(date) FROM (`userhealthinfo`) WHERE uid="'.$uid.'")';
		$query = $this->db->query($sql);
		
		if(!$query->num_rows())
			return null;
		
		return $query->row();
	}

	public function get_symptoms($uid)
	{
		$symptoms = array();
		$this->load->model('personal_health_info_model');
		$rows = $this->get_last_symptoms($uid);

		if($rows == null)
		{
			return $symptoms;
		}

		foreach($rows as $row)
		{
			array_push($symptoms, get_symptom_name_by($row->symptomType));
		}

		return $symptoms;
	}
	
	public function is_symptom_shared_by($uid, $symptom_type)
	{
		$rows = $this->get_last_symptoms($uid);
		if(!$rows)
			return false;
	
		for($symptom = current($rows); $symptom; $symptom = next($rows))
		{
			if($symptom->symptomType == $symptom_type)
			{
				return $symptom->share;
			}
		}
	
		return false;
	}
	
	public function get_shared_symptom($uid)
	{
		$rows = $this->get_last_symptoms($uid);
		if(!$rows)
			return null;
		
		$shared_symptoms = array();
		for($symptom = current($rows); $symptom; $symptom = next($rows))
		{
			if(!$symptom->share){
				continue;
			}
			
			$shared_symptoms[] = $symptom;
		}
		
		return $shared_symptoms;
	}
	
	public function get_all_shared_symptoms($uid)
	{
		$rows = $this->get_last_symptoms($uid);
		
		if(!$rows)
			return array();
		
		$res = array();
		foreach($rows as $r)
		{
			if(!$r->share)
				continue;
			
			$res[] = $r;
		}
		
		return $res;
	}

	public function get_symptoms_share($uid)
	{
		$symptoms = array();
		$this->load->model('personal_health_info_model');
		$rows = $this->get_last_symptoms($uid);

		if($rows == null)
		{
			return $symptoms;
		}

		foreach($rows as $row)
		{
			$symptoms[get_symptom_name_by($row->symptomType)]=$row->share;
		}

		return $symptoms;
	}

	public function delete_symptom($uid, $symptom_id)
	{
		if($symptom_id < 0)
			return;


		$sql = 'SELECT * FROM (`usersymptom`) WHERE symptomType="'.$symptom_id.'" AND uid="'.$uid.'" AND endDate is NULL';
		$query = $this->db->query($sql);
		
		if(!$query->num_rows())
			return;
		
		$row = $query->row();
		$symptomInfoId = $row->symptomInfoId;
		$share = $row->share;

		//$sql = 'UPDATE (`usersymptom`) SET endDate="'.mysql_datetime().'" WHERE symptomType="'.$symptom_id.'" AND uid="'.$uid.'" AND endDate is NULL';
		$data = array(
				'endDate' => mysql_datetime()
		);

		$where = array(
				'symptomType'=>$symptom_id,
				'uid'=>$uid,
				'endDate'=>NULL
		);

		//$sql = 'symptomType="'.$symptom_id.'" AND uid="'.$uid.'" AND endDate is NULL';
		$this->db->update('usersymptom', $data, $where);

		if($share)
		{
			$this->notice_symptom_to_friends($symptomInfoId);
		}
	}

	public function share_symptom($uid, $symptom_id, $share)
	{
		if($symptom_id < 0)
			return;

		$f = 0;
		if($share == "true")
		{
			$f = 1;
		}


		$sql = 'SELECT * FROM (`usersymptom`) WHERE symptomType="'.$symptom_id.'" AND uid="'.$uid.'" AND endDate is NULL';
		$symptomInfoId = $query = $this->db->query($sql)->row()->symptomInfoId;

		//$sql = 'UPDATE (`usersymptom`) SET endDate="'.mysql_datetime().'" WHERE symptomType="'.$symptom_id.'" AND uid="'.$uid.'" AND endDate is NULL';
		$data = array(
				'share' => $f
		);

		$where = array(
				'symptomType'=>$symptom_id,
				'uid'=>$uid,
				'endDate'=>NULL
		);

		//$sql = 'symptomType="'.$symptom_id.'" AND uid="'.$uid.'" AND endDate is NULL';
		$this->db->update('usersymptom', $data, $where);

		if($share == "true")
		{
			$this->notice_symptom_to_friends($symptomInfoId);
		}
	}

	public function add_symptom($uid, $symptom_id, $share)
	{
		if($symptom_id < 0)
			return;

		$datetime = mysql_datetime();
		$this->db->set('uid', $uid);
		$this->db->set('symptomType', $symptom_id);
		$this->db->set('startDate', $datetime);
		$this->db->set('share', $share);
		$this->db->insert('usersymptom');
		
		$this->remove_conflict_symptom($uid, get_symptom_name_by($symptom_id));
		 
		//$symptomInfoId = $this->db->insert_id();
		//$this->notice_symptom_to_friends($symptomInfoId);
		 
	}
	
	public function remove_conflict_symptom($uid, $symptom_name)
	{
		$conflict_symptoms = get_conflict_symptom($symptom_name);
		
		foreach($conflict_symptoms as $symptom_name)
		{
			$symptom_id = get_symptom_id_by($symptom_name);
			$this->delete_symptom($uid, $symptom_id);
		}
		
	}

	public function get_analyse_result($uid)
	{
		$results = array();
		$row = $this->get_current_health_info($uid);

		if($row == null)
		{
			return $results;
		}
		
		$bloodPressureHigh = null;
		$bloodPressureLow = null;

		$symptom_map = get_symptom_map();
		foreach ($row as $key=>$value)
		{
			if($value != null)
			{
				if($key == 'totalCholesterol')
				{
					if($value > 200 && $value <= 239)
					{
						array_push($results, $symptom_map[0]);
					}
					else if($value > 239)
					{
						array_push($results, $symptom_map[1]);
					}
				}
				else if($key == 'lowDensityLipoprotein')
				{
					if($value > 130 && $value <= 160)
					{
						array_push($results, $symptom_map[2]);
					}
					else if($value > 160)
					{
						array_push($results, $symptom_map[3]);
					}
				}
				else if($key == 'highDensityLipoprotein')
				{
					if($value < 40)
					{
						array_push($results, $symptom_map[4]);
					}
				}
				else if($key == 'triglyceride')
				{
					if($value > 150 && $value <= 199)
					{
						array_push($results, $symptom_map[5]);
					}
					else if($value > 199)
					{
						array_push($results, $symptom_map[6]);
					}
				}
				else if($key == 'fastingBloodSugar')
				{
					if($value > 100 && $value <= 125)
					{
						array_push($results, $symptom_map[7]);
					}
					else if($value > 125)
					{
						array_push($results, $symptom_map[8]);
					}
				}
				else if($key == 'normalBloodSugar')
				{
					if($value > 200)
					{
						array_push($results, $symptom_map[9]);
					}

				}
				else if($key == 'bloodPressureHigh')
				{
					$bloodPressureHigh = $value;
					if($bloodPressureLow != null)
					{
						if(($bloodPressureHigh >=120 && $bloodPressureHigh <=139) &&
								($bloodPressureLow >=80 && $bloodPressureLow <=89))
						{
							array_push($results, $symptom_map[10]);
						}
						else if(($bloodPressureHigh >=140 && $bloodPressureHigh <=159) &&
								($bloodPressureLow >=90 && $bloodPressureLow <=114))
						{
							array_push($results, $symptom_map[11]);

						}
						else if($bloodPressureHigh >=160 && $bloodPressureLow >=115)
						{
							array_push($results, $symptom_map[12]);
						}
					}
				}
				else if($key == 'bloodPressureLow')
				{
					$bloodPressureLow = $value;
					if($bloodPressureHigh != null)
					{
						if(($bloodPressureHigh >=120 && $bloodPressureHigh <=139) &&
								($bloodPressureLow >=80 && $bloodPressureLow <=89))
						{
							array_push($results, $symptom_map[10]);
						}
						else if(($bloodPressureHigh >=140 && $bloodPressureHigh <=159) &&
								($bloodPressureLow >=90 && $bloodPressureLow <=114))
						{
							array_push($results, $symptom_map[11]);

						}
						else if($bloodPressureHigh >=160 && $bloodPressureLow >=115)
						{
							array_push($results, $symptom_map[12]);
						}
					}
				}
				else if($key == "height")
				{
					$height = ((float)$row->height) / 100.0;						
						
					if($row->weight != null)
					{
						$weight = (float)$row->weight;														
							
						$BMI = $weight / ($height * $height);
							
						if($BMI < 18.5)
						{
							array_push($results, $symptom_map[13]);
						}
						else if($BMI >= 18.5 && $BMI <= 24.9)
						{
							//normal, do nothing
						}
						else if($BMI >=25 && $BMI <= 29.9)
						{
							array_push($results, $symptom_map[14]);
						}
						else if($BMI > 30)
						{
							array_push($results, $symptom_map[15]);
						}
					}
						
				}
				//TODO: Other justifications
			}
		}

		return $results;
	}

	public function notice_symptom_to_friends($symptom_info_id)
	{
		$uid = get_uid();
		$sql = 'SELECT * FROM (`userfriends`) WHERE uid="'.$uid.'"';
		$query = $this->db->query($sql);
		$rows = $query->result();

		foreach($rows as $row)
		{
			$friend_uid = $row->friendUid;
			$this->push_symptom_to_friend($uid, $friend_uid, $symptom_info_id);
		}
	}
	
	public function push_symptom_to_friend($uid, $friend_uid, $symptom_info_id)
	{
		$this->db->set("uid", $uid);
		$this->db->set("friendUid", $friend_uid);
		$this->db->set("symptomInfoId", $symptom_info_id);
		$this->db->insert("usersymptompushnotice");
	}
}
?>